import { _decorator, Component, Label, Node } from 'cc';
import { GameManager } from './GameManager';
import { GameState, PropType } from './constants/GameConstants';
import { global } from './common/Global';
const { ccclass, property } = _decorator;

@ccclass('AdController')
export class AdController extends Component {
   
    @property({type: Label})
    private timeNode : Label = null;

    @property({type: Number})
    private num : number = 0;

    private n : number = 0;
 
    onLoad(){
        this.init();
    }
 


    init(){
        this.timeNode.getComponent(Label).string = this.num.toString()
    }


    /**
     * Node每次激活时执行
     */
    onEnable() {
        this.init();
        this.n = this.num;
        const callback = function () {
            if(this.n > 0){
                this.n--
                this.timeNode.string = this.n.toString();
            }else{
                this.unschedule(this);
            }
        }
        this.schedule(callback, 1);
    }


  
    close(): void {
        this.node.active = false
        if(this.n > 0 ) {
            console.log("未看完广告, 无法领取奖励")
        }else{
            console.log("已看完广告,发放奖励")
            global.addProp(PropType.RED_PACK, 1)
        }
        GameManager.Instance.setGameState(GameState.GS_PLAYING)
    }

}

